package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.myapplication.socket.UdpReceiveThread;
import com.example.myapplication.socket.UdpSendThread;
import com.example.myapplication.tools.Commons;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private Button searchWifiInitDeviceInfoBtn;
    private TextView wifiInitDeviceNameTv;
    private TextView wifiInitDeviceIPTv;
    private TextView wifiInitlocalhostDeviceIPTv;
    private Handler mHandler;
    private Button getDeviceDataBtn;
    private UdpSendThread udpSendThread;
    private UdpReceiveThread udpReceiveThread;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg){
                if(msg.what == 0x123){
                    String result = (String) msg.obj;
                    String[] resultStringArray = result.split(",");
                    wifiInitDeviceNameTv.setText(resultStringArray[2] + "");
                    wifiInitDeviceIPTv.setText(resultStringArray[0] + "");

                }
            }
        };
    }

    public void initView() {
        searchWifiInitDeviceInfoBtn = findViewById(R.id.search_wifi_device_info_init_btn);
        wifiInitDeviceNameTv = findViewById(R.id.init_device_name_tv);
        wifiInitDeviceIPTv = findViewById(R.id.init_device_IP_tv);
        wifiInitlocalhostDeviceIPTv = findViewById(R.id.init_localhost_IP_tv);

        getDeviceDataBtn = findViewById(R.id.get_device_data_btn);

        wifiInitlocalhostDeviceIPTv.setText(Commons.getIpAddressString() + "");

    }

    public void initData() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("dsa","");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        String url = "http://test.usr.cn/1.php?data=ABC";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("收到的数据", "onResponse: " + jsonObject.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MainActivity.this, volleyError.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    public void initEvent() {
        searchWifiInitDeviceInfoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //开启发送UPD数据线程
                udpSendThread = new UdpSendThread(mHandler);
                udpSendThread.start();
                //开启接收UPD数据线程
                udpReceiveThread = new UdpReceiveThread(mHandler);
                udpReceiveThread.start();
            }
        });
        getDeviceDataBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,DeviceDataActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}